import random
from user import User
from addr import Addr


class Operate:
    def __init__(self, userinfo={}, useraddr={}):
        self.userinfo = userinfo
        self.useraddr = useraddr

    def adduser(self):
        '''
        添加用户
        :param amount:
        :return:
        '''
        id = str(random.randint(0, 99999999)).zfill(8)
        name = input('请输入姓名:')
        pwd = input('请输入密码:')
        balance = 0
        bankname = "中国工商银行"
        country = input('输入你的国家:')
        province = input('输入你的省份:')
        street = input('输入你的街道:')
        number = input('输入你的门牌号:')
        user = User(id, name, pwd, balance, bankname)
        addr = Addr(country, province, street, number)
        if len(self.userinfo) >= 100:
            return 3
        elif id in self.userinfo.keys():
            return 2
        else:
            self.userinfo[id] = user
            self.useraddr[id] = addr
            print("你的账户是：", user.id)
            return 1

    def deposit(self):
        '''
        存钱
        :param amount:
        :return:
        '''
        id = input('请输入您的卡号:')
        user = self.userinfo.get(id)
        if user:
            money = int(input('请输入要存的金额:'))
            user.balance += money
        else:
            return False

    def withdrawal(self):
        '''
        取钱
        :param amount:
        :return:
        '''
        id = input('请输入您的卡号:')
        user = self.userinfo.get(id)
        if user:
            pwd = input('请输入密码:')
            if user.pwd == pwd:
                money = int(input('请输入要取的金额:'))
                if user.balance > money:
                    user.balance -= money
                    print("拿好你的￥", money)
                else:
                    return 3
            else:
                return 2
        else:
            return 1

    def transfer(self):
        '''
        转账
        :param amount:
        :return:
        '''
        id = input('请输入您的卡号:')
        user = self.userinfo.get(id)
        if user:
            dst_id = input('请输入对方卡号:')
            dst_user = self.userinfo.get(dst_id)
            if dst_user:
                money = int(input('请输入转账金额:'))
                if user.balance >= money:
                    pwd = input('请输入密码:')
                    if user.pwd == pwd:
                        user.balance -= money
                        dst_user.balance += money
                        print("转账成功")
                    else:
                        return 2
                else:
                    return 3
            else:
                return 1
        else:
            return 1

    def check(self):
        '''
        查询
        :param amount:
        :return:
        '''
        id = input('请输入您的卡号:')
        user = self.userinfo.get(id)
        addr = self.useraddr.get(id)
        if user:
            pwd = input('请输入密码:')
            if user.pwd == pwd:
                print("当前账户：", user.id, "密码：", user.pwd, "余额：", user.balance, "元 用户居住地：", addr.country, addr.province,
                      addr.street, addr.number, "当前开户行：", user.bankname)
            else:
                print("密码错误")
        else:
            print("该用户不存在")
